#region Using

using System.Collections.ObjectModel;

#endregion

namespace XNgine.Misc
{
    public class NamedItemCollection<T> : KeyedCollection<string, T> where T : INamedItem
    {
        /// <summary>
        /// Allow us to get an element by name 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        protected override string GetKeyForItem(T item)
        {
            return item.Name;
        }

        public T getItemAt(int index)
        {
            return Items[index];
        }

        public T GetLast()
        {
            if (Items.Count == 0) return default(T);
            return Items[Items.Count - 1];
        }

        public T GetFirst()
        {
            if (Items.Count == 0) return default(T);
            return Items[0];
        }

        /// <summary>
        /// Replaces the inherited behavior of Contains(item) for performance reasons
        /// Inherited implementation is O(n), while this one is O(1)
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        new public bool Contains(T item)
        {
            return Contains(item.Name);
        }
    }
}